package com.ktwlsoft.archivemanage.domain.business.repositoryService;

import com.ktwlsoft.archivemanage.domain.dao.entity.Infrastructure;
import com.ktwlsoft.archivemanage.domain.dao.repository.InfrastructureMapper;
import com.ktwlsoft.archivemanage.payload.Result;
import com.ktwlsoft.archivemanage.payload.ResultType;
import com.ktwlsoft.archivemanage.payload.inforConst.InfrastructureAddRequest;
import com.ktwlsoft.archivemanage.payload.inforConst.InfrastructureUpdateRequest;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author chenkun
 * @version 1.0
 * @description 基础设施数据层服务类
 * @date 2019-10-28
 */
@Service
public class InfrastructureRepositoryService {

    @Autowired
    private InfrastructureMapper infrastructureMapper;

    /**
     * @param unitId 立档单位id
     * @return Result
     * @description 查询基础设施信息
     */
    public Result findInfrastructureInfo(Long unitId) {
        if (unitId == null) {
            return Result.body(null, ResultType.BUSINESS_ERROR).withMessage("立档单位信息不存在");
        }
        return Result.body(infrastructureMapper.findInfrastructureInfo(unitId), ResultType.BUSINESS_SUCCESS);
    }

    /**
     * @param request 基础设施数据
     * @return Result
     * @description 新增基础设施信息
     */
    public Result addInfrastructureInfo(InfrastructureAddRequest request) {
        Infrastructure infrastructure = new Infrastructure();
//        Long unitId=request.getUnitId();
        BeanUtils.copyProperties(request, infrastructure);
//        if(null == unitId || "".equals(unitId)){
//            return Result.body(null, ResultType.BUSINESS_ERROR).withMessage("立档单位信息不存在");
//        }
        try {
            infrastructureMapper.addInfrastructureInfo(infrastructure);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.body(null, ResultType.SAVING_ERROR).withMessage(e.getMessage());
        }
        return Result.body(infrastructure.getId(), ResultType.BUSINESS_SUCCESS).withMessage("新增基础设施信息成功");
    }

    /**
     * @param infrastructure 基础设施数据
     * @return Result
     * @description 新增基础设施信息
     */
    public Result addInfrastructureInfo(Infrastructure infrastructure) {
        try {
            infrastructureMapper.addInfrastructureInfo(infrastructure);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.body(null, ResultType.SAVING_ERROR).withMessage(e.getMessage());
        }
        return Result.body(infrastructure.getId(), ResultType.BUSINESS_SUCCESS).withMessage("新增基础设施信息成功");
    }

    /**
     * @param request 基础设施数据
     * @return Result
     * @description 更新基础设施信息
     */
    public Result updateInfrastructureInfo(InfrastructureUpdateRequest request) {
//        Long unitId=request.getUnitId();
        Infrastructure infrastructure = new Infrastructure();
        BeanUtils.copyProperties(request, infrastructure);
//        if(null == unitId || "".equals(unitId)){
//            return Result.body(null, ResultType.BUSINESS_ERROR).withMessage("立档单位信息不存在");
//        }
        try {
            infrastructureMapper.updateInfrastructureInfo(infrastructure);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.body(null, ResultType.UPDATE_ERROR).withMessage(e.getMessage());
        }
        return Result.body(infrastructure.getId(), ResultType.BUSINESS_SUCCESS).withMessage("更新基础设施信息成功");
    }

    /**
     * @param infrastructure 基础设施数据
     * @return Result
     * @description 更新基础设施信息
     */
    public Result updateInfrastructureInfo(Infrastructure infrastructure) {
        try {
            infrastructureMapper.updateInfrastructureInfo(infrastructure);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.body(null, ResultType.UPDATE_ERROR).withMessage(e.getMessage());
        }
        return Result.body(infrastructure.getId(), ResultType.BUSINESS_SUCCESS).withMessage("更新基础设施信息成功");
    }

    /**
     * @param unitId 立档单位id
     * @return int
     * @description 删除基础设施信息
     */
    public int deleteInfrastructureInfo(Long unitId) {
        return infrastructureMapper.deleteInfrastructureInfo(unitId);
    }

    /**
     * 根据立档ID查询信息 on 2019-10-24 14:19:42
     *
     * @param unitId unitId
     * @return Infrastructure
     */
    public Infrastructure getByUnitId(Long unitId) {
        return infrastructureMapper.getByUnitId(unitId);
    }

    /**
     * 修改 on 2019/11/1 15:55
     *
     * @param infrastructure infrastructure
     * @return int
     */
    public int updateByPrimaryKeySelective(Infrastructure infrastructure) {
        return infrastructureMapper.updateByPrimaryKeySelective(infrastructure);
    }
}
